package Control;

import Database.DBProduct;
import Model.Product;

public class ProductCtrl {
    Product productModel;

    DBProduct product = new DBProduct();

    public String createProduct(int productID, String name, double purchasePrice, double salesPrice, String countryOfOrigin, int minStock, int supplierID) {
	String message = null;
	if (productID != 0 && name != null && purchasePrice != 0 && salesPrice != 0 && countryOfOrigin != null && minStock != 0 && supplierID != 0) {

	    if (product.findProduct(productID).getProductID() != productID) {
		productModel = new Product(productID, name, purchasePrice, salesPrice, countryOfOrigin, minStock, supplierID);

		try {
		    product.insertProduct(productModel);
		    message = "Product: " + name + " was created succesfuly!";
		} catch (Exception e) {

		    e.printStackTrace();
		}

	    } else {
		// Sent a message to the user that the input was wrong
		message = "Product with this ID[" + productID + "] already exist!";
	    }
	} else {
	    message = "Please fill up all fields!";

	}
	return message;
    }

    public String findProduct(int productID) {
	Product product = null;
	String message = null;
	try {
	    product = product.findProduct(productID);
	    System.out.println(product.getName());
	} catch (Exception e) {

	    System.out.println("can't find");
	}

	if (product.getProductID() == 0) { // id 0 must not exist

	    message = "Can't find product!";
	} else {

	    message = "Product " + product.getName() + " [ID - " + product.getProductID() + "] was found";
	}
	// Sent a message to the user that the input was wrong or the product
	// was not found
	return message;

    }

    public String updateProduct(int productID, String name, double purchasePrice, double salesPrice, String countryOfOrigin, int minStock, int supplierID) {
	String message = null;
	if (product.findProduct(productID).getProductID() == productID) {
	    productModel = new Product(productID, name, purchasePrice, salesPrice, countryOfOrigin, minStock, supplierID);
	    product.updateProduct(productModel);
	    message = name + " was updated succesfuly!";
	} else {

	    message = "Product doesn't exist!";
	}
	// We should either prompt for what information to update
	// or if we should force an update in all fields of the product
	return message;
    }

    public String deleteProduct(int productID) {

	String message = null;
	if (product.findProduct(productID).getProductID() == productID) {
	    String name = product.findProduct(productID).getName();
	    product.deleteProduct(productID);

	    message = name + " was deleted succesfuly!";
	} else {

	    message = "Product doesn't exist!";
	}
	// Here we need a script that removes the information
	// from the database

	return message;
    }

}
